package fr.umlv.metadata;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import fr.umlv.authentication.SessionHandler;

// TODO: Auto-generated Javadoc
/**
 * The Class WebServiceUtils.
 */
public class WebServiceUtils {
	
	/**
	 * Gets the service.
	 *
	 * @param urlpath the urlpath
	 * @return the service
	 */
	public static org.w3c.dom.Document getService(String urlpath){
		Document result = null;
		try{
			String urlToConnect = urlpath.replace(" ", "%20");
			URL urlCateg = new URL(urlToConnect);
			URLConnection urlCategConnection = urlCateg.openConnection();
			String userPassword = SessionHandler.getClientSession().getSessionParameters().get(SessionParameter.USER) + ":" + SessionHandler.getClientSession().getSessionParameters().get(SessionParameter.PASSWORD);
			String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
			urlCategConnection.addRequestProperty("Authorization", "Basic " + encoding);
		    InputStream stream = urlCategConnection.getInputStream();
		       
		    
		    DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
			DocumentBuilder constructeur = fabrique.newDocumentBuilder();
			
			result = constructeur.parse(stream);
			
		}catch(MalformedURLException e){
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	/**
	 * Gets the json service.
	 *
	 * @param urlpath the urlpath
	 * @return the json service
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws ParseException the parse exception
	 */
	public static JSONObject getJsonService(String urlpath) throws IOException, ParseException {
		String urlToConnect = urlpath.replace(" ", "%20");
		URL urlCateg = new URL(urlToConnect);
		URLConnection urlCategConnection = urlCateg.openConnection();
		String userPassword = SessionHandler.getClientSession().getSessionParameters().get(SessionParameter.USER) + ":" + SessionHandler.getClientSession().getSessionParameters().get(SessionParameter.PASSWORD);
		String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
		
		urlCategConnection.setRequestProperty("Authorization", "Basic " + encoding);
	    InputStream stream = urlCategConnection.getInputStream();
	    
	    
	    int len;
	     char[] cBuf = new char[1024];
	     StringBuffer sb = new StringBuffer();
	       
	     BufferedReader reader  = new BufferedReader(new InputStreamReader(stream));
	     while ((len=reader.read(cBuf,0,1024)) != -1) {
	          sb.append(cBuf,0,len);
	     }
	     reader.close();

	    String result = sb.toString();
	       
	    JSONObject jsonobject = new JSONObject(result);
	    
	       
	    return jsonobject;     
	}
}
